package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.dto.ProductDTO;
import com.qf.entity.Product;
import com.qf.exception.JxcException;
import com.qf.mapper.ProductMapper;
import com.qf.service.ProductService;
import com.qf.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<ProductDTO> findAll() {
        //1. 查询商品
        List<Product> productList = productMapper.selectAll();
        //2. 健壮性判断
        if(productList == null || productList.size() <= 0){
            log.error("【查询全部商品】 查询商品失败!! productList = {}",productList);
            throw new JxcException(666,"查询商品失败!!");
        }
        //3. 返回数据
        List<ProductDTO> result = new ArrayList<>();
        for (Product product : productList) {
            ProductDTO dto = new ProductDTO(product.getId(),product.getProductName());
            result.add(dto);
        }
        return result;
    }
}
